<?php
/**
 * Created by PhpStorm.
 * User: yy
 * Date: 2018/4/21
 * Time: 10:06
 */

namespace app\api\controller;

use app\api\model\User as UserModel;
use think\Exception;
use think\Request;

class User
{
    //登录
    public function getUser()
    {
        //        $user = UserModel::getUserName($id);
        $user = new UserModel();
        $request = Request::instance();
        $num = $request->param('num');
        $pw = $request->param('pw');
        $result = [
            'result' => '',
            'msg' => '',
            'status' => true
        ];
        if (empty($num) || empty($pw)) {
            $result['msg'] = '登陆失败233';
            $result['status'] = false;
            return $result;
        }
        $user = $user->where('account_number', '=', $num)->where('pw', '=', md5($pw))->find();
        if (!empty($user)) {
            $result['msg'] = '登陆成功';
            $user->last_time = date("Y-m-d H:i:s");
            $user->save();
        } else {
            $result['msg'] = '登陆失败';
            $result['status'] = false;
        }
        return json($result);
    }

    //添加账号
    public function addUser()
    {
        $request = Request::instance();
        $num = $request->param('num');
        $pw = $request->param('pw');
        //        throw new Exception('什么鬼',233);
        $result = [
            'result' => '',
            'msg' => '',
            'status' => true
        ];
        $user = new UserModel();
        $res = $user->where('account_number', '=', $num)->find();
        if ($res) {
            $result['msg'] = '账号已注册或账号不能为空';
            $result['status'] = false;
            return json($result);
        } else {
            $user->data([
                'account_number' => $num,
                'pw' => md5($pw),
                'create_time' => date("Y-m-d H:i:s")
            ]);
            $res = $user->save();
        }
        if ($res == 1) {
            $result['msg'] = '注册成功';

            return json($result);

        }
        $result['status'] = false;
        $result['msg'] = '注册失败';

        return json($result);

    }

    public function userList()
    {
        $result = [
            'result' => null,
            'msg' => '',
            'status' => true
        ];
        $res = UserModel::field('account_number,create_time,last_time,id')->select();
//        $user = new UserModel();
//        $res = $user->field('account_number,create_time')->select();
        if($res){
            $result['msg'] = '查询成功';
            $result['result'] = $res;
        }else{
            $result['msg'] = '查询失败';
            $result['status'] = false;
        }
        //转换成数组
//        $resArr = collection($res)->toArray();
//        if(count($resArr)>0){
//
//        }
        return $result;
    }
    public function userDel(){
        $result = [
            'result' => null,
            'msg' => '',
            'status' => true
        ];
        $id = Request::instance()->param('id');
        $user = UserModel::get($id);
        if(!$user){
            $result['status']=false;
            $result['msg']='该用户不存在或数据库错误';
            return $result;
        }
        $res = $user->delete();
        if($res != 1){
            $result['status']=false;
            $result['msg']='删除失败';
        }else{
            $result['msg']='删除成功';
        }
        return $result;
    }
}